from traceback import print_tb
import numpy as np
import sys
import random
import time
from PIL import Image, ImageDraw
import os
import cv2


im = Image.open("output_rgb.bmp")

im_hide = Image.open("dct.bmp")
# im_array = np.array(im_hide)
# im_array = np.reshape(im_array, (-1, 3)) # 变二维数组
# print(im_array)
# print(im_array[0][0])
# for i in im_array:
#     print(i)
# print(im_array[0][0])
# print(im_array[0][1])

width, height = im_hide.size

extract_list = list()

out = Image.new('RGB', (width, height))  # 输出图像
draw = ImageDraw.Draw(out)  # 画图

def dct():
    cnt = 0  #用于记录被隐藏图片图片像素点数，写入一个就自增, 每128就清零
    for k in range(64):
        for l in range(64):
            dct_array = np.zeros(64).reshape(8, 8)
            for y in range(8):
                for x in range(8):
                    pix = im.getpixel((k * 8 + x, l * 8 + y))  # 依次获取原图像素点                  
                    dct_array[x][y] = pix[2]

            print("dct前88矩阵:")
            print(dct_array)

            dct_dct = cv2.dct(dct_array) # dct变换
            print("dct矩阵:")
            print(dct_dct)

            # 开始提取数据
            # print(dct_dct[3][3])
            # print(dct_dct[3][4])
            # print(dct_dct[4][3])
            # print(dct_dct[4][4])
            extract_list.append(int(dct_dct[3][3]))
            extract_list.append(int(dct_dct[3][4]))
            extract_list.append(int(dct_dct[4][3]))
            extract_list.append(int(dct_dct[4][4]))
            cnt += 1
            print("进度:" + str(cnt))

            # for y in range(8):
            #     for x in range(8):
            #         pix = im.getpixel((k * 8 + x, l * 8 + y))  # 依次获取原图像素点            
            #         co_list = list()
            #         co_list.append(int(pix[0]))
            #         co_list.append(int(pix[1]))
            #         co_list.append(int(dct_idct[x][y]))
            #         co_tuple = tuple(co_list)
            #         draw.point((k * 8 + x, l * 8 + y), fill=co_tuple)  # 颜色元组，并且在（x,y）上画一个像素点
            #         out.save("output_rgb", 'bmp') # 保存到的图片的名称



def draw_bmp():
    cnt = 0
    for y in range(height):
        for x in range(width):
            co_list = list()
            co_list.append(int(extract_list[cnt]))
            co_list.append(int(extract_list[cnt]))
            co_list.append(int(extract_list[cnt]))
            co_tuple = tuple(co_list)
            print("(" + str(x) + "," + str(y) + ")")
            print(co_tuple)
            draw.point((x, y), fill = co_tuple)  # 颜色元组，并且在（x,y）上画一个像素点
            out.save("extract", 'bmp') # 保存到的图片的名称
            cnt += 1
            


dct()
draw_bmp()